package algorithm.sort;

//选择排序
public class ChooseSort {
    public static void main(String[] args) {
        int[] data = new int[]{-99,-6,3,78,9,40};

        sort2(data);

        for (int i = 0; i < data.length; i++) {
            System.out.print(data[i]+" ");
        }
    }

    public static void sort1(int[] data){
        for (int i =0;i<data.length-1;i++){
            for (int j =i+1;j<data.length;j++){
                if (data[i]>data[j]){
                    int temp = data[i];
                    data[i] = data[j];
                    data[j]= temp;
                }
            }
        }
    }

    //选择排序的优化
    public static void sort2(int[] data) {
        for (int i =0;i<data.length-1;i++){
            int min = i; //假设最小值的下标是当前的i
            for (int j =i+1 ;j<data.length;j++){
                if (data[min]>data[j]){
                    min = j;
                }
            }
            if (min!=i){
                int temp = data[min];
                data[min]=data[i];
                data[i] = temp;
            }

        }
    }
}
